//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Expressions
{
    class FloatingRowReference: RowReference
    {
        public FloatingRowReference(Worksheet worksheet, int offset, bool isLocked)
        {
            _offset = offset;
            _worksheet = worksheet;
            this._isLocked = isLocked;
        }

        internal override DimensionReference<Column, Row> SwapDimension()
        {
            return new FloatingColumnReference(Worksheet, _offset, IsLocked);
        }

        internal override bool IsLocked
        {
            get { return _isLocked; }
        }

        internal override string GetText(TextContext context)
        {
            Cell c = context.EvaluationContext.Target;
            int row = c.Row.Index + context.EvaluationContext.Offset.Row + _offset;
            return c.Worksheet.Rows[row].Label;
        }

        internal override RowReference Projected(Worksheet targetWorksheet)
        {
            return new FloatingRowReference(targetWorksheet, _offset, IsLocked);
        }

        internal int RowOffset { get { return _offset; } }
        readonly int _offset;
        readonly Worksheet _worksheet;
        private readonly bool _isLocked;

        internal override Row GetRow(EvaluationContext context)
        {
            Contract.Requires(context != null);
            Contract.Requires(context.Target != null);
            return _worksheet.Rows[context.Target.Row.Index + _offset];
        }
        internal override RowReference Offset(CellRelativeAddress address)
        {
            if (IsLocked)
            return this;
            else
            {
                return new FloatingRowReference(this.Worksheet, _offset + address.Row, _isLocked);
            }
        }
        public override Worksheet Worksheet
        {
            get { return _worksheet; }
        }

        public override string Label
        {
            get { return "R[" + _offset+"]"; }
        }

        public override Row Anchor
        {
            get
            {
                throw new NotImplementedException();
            }
        }
    }
}
